package com.example.tys.baseapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.camera.camerview.CameraView;

import static com.google.android.camera.camerview.CameraView.FLASH_OFF;

public class CameraTestActivity extends AppCompatActivity {

    private CameraView mCameraView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_test);
        mCameraView = findViewById(R.id.camera);
        if (mCameraView != null) {
            mCameraView.addCallback(new CameraView.Callback() {
                @Override
                public void onCameraOpened(CameraView cameraView) {
                    super.onCameraOpened(cameraView);
                    Log.e("CameraTestActivity", "onCameraOpened:");
                }

                @Override
                public void onCameraClosed(CameraView cameraView) {
                    super.onCameraClosed(cameraView);
                    Log.e("CameraTestActivity", "onCameraClosed:");
                }

                @Override
                public void onPictureTaken(CameraView cameraView, byte[] data) {
                    super.onPictureTaken(cameraView, data);
                    Log.e("CameraTestActivity", "onPictureTaken:" + data.toString() + " "
                    +Thread.currentThread().getName()
                    );
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCameraView.start();
    }

    @Override
    protected void onPause() {
        mCameraView.stop();
        super.onPause();
    }

    public void b(View view) {
        if (mCameraView.getFlash() == CameraView.FLASH_TORCH) {
            mCameraView.setFlash(CameraView.FLASH_OFF);
        } else {
            mCameraView.setFlash(CameraView.FLASH_TORCH);
        }
    }
}
